#!/usr/bin/webif-page
<? 
. /usr/lib/webif/webif.sh
load_settings charon
. /etc/default/charon

header "Charon" "Prepaid" "@TR<<Prepaid Configuration>>" '' "$SCRIPT_NAME"

fullform() {
display_form <<EOF
start_form|@TR<<Prepaid Deposit>>
field|@TR<<Customer's PIN>>
text|pin2|$FORM_pin2
field|@TR<<Deposit Amount>>
text|amount|$FORM_amount
select|currency|$FORM_currency
option|AUD|@TR<<Australian Dollars>>
option|EUR|@TR<<Euros>>
option|USD|@TR<<US Dollars>>
submit|deposit|@TR<<Confirm Payment>>
end_form
EOF
}

( ! empty $FORM_lookup ) && {
    validate <<EOF
int|FORM_pin|@TR<<PIN>>|required|$FORM_pin
EOF
equal $? 0 && {
if /usr/bin/prepaid get keys $FORM_pin >/dev/null 2>&1; then
FORM_pin2=$FORM_pin
FORM_currency=$CHARON_CURRENCY
fullform
else
ERROR="Invalid PIN"
fi
}
}

if ! empty $FORM_deposit; then
	case "$FORM_currency" in 
		AUD|EUR|USD)
			true;;
		*)
			ERROR="Invalid currency: try AUD, EUR or USD";;
	esac
	VALIDATED=`echo "$FORM_amount" | sed '/^[0-9][0-9]*\.[0-9][0-9]*$/{p;d}/^[0-9]*$/p;d'`
	if [ "$VALIDATED" = "$FORM_amount" ]; then
		true
	else
		ERROR="Invalid amount: valid format is '1.23', '43.21', etc"
	fi
	if empty $FORM_amount; then
		ERROR="No payment amount supplied"
	fi
	if empty $ERROR; then
		/usr/bin/prepaid pay $FORM_pin2 $FORM_currency $FORM_amount 2>/dev/null
		if equal $? 0; then
			echo "<H2>Payment accepted. Advise the customer to complete the transfer through the website.</H2>"
		else
			ERROR="Generating payment failed. Please check data and try again."
		fi
	fi
	if ! empty $ERROR; then
	    echo "$ERROR"
	    fullform
	    ERROR=""
	fi
fi

display_form <<EOF
start_form|@TR<<PIN Lookup>>
field|@TR<<Customer's PIN>>
text|pin
submit|lookup|@TR<<Check PIN>>
end_form
EOF

echo "${ERROR}"
?>


<!--
##WEBIF:name:Charon:3:Prepaid
-->
